<?php
/**
* @filename course_note.php 2016-9-6 02:10:56x
* @author Yunfent Zhang <306232452@qq.com>
* @version 1.0.0
* @copyright DZF (c) 2016, Yunfeng Zhang
*/
class ctrl_questions_answer extends questions_answer {
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	
	static function &instance() {
		static $object;
		if (empty($object)) {
			$object = new self();
		}
		return $object;
	}
	
/*
	*  问答信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '10';
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
        if($user_id){
        	$wheresql .= " AND user_id = '".$user_id."' ";
        }
        $q_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if($q_id){
            $wheresql .= " AND q_id = '".$q_id."' ";
        }
        //课时id
        $ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
        
        if($ch_id){
        	$wheresql .= " AND ch_id = ".$ch_id;
        }
        $orderby = " ORDER BY q_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );              
        $page_result = $this->index('questions',$page_condition);     
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $q_id_array = array();
             
        //DEBUG 返回输出
        $api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
        $client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data']=$page_result['page_data'];
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $return['code'] = '1';
            $return['data']=array();
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);    
        }
	}
	
	/*
	 *  用户发起回答
	 */
	public function do_add() {
		global $_G;
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$answer = isset($_REQUEST['answer']) && !empty($_REQUEST['answer']) ? $_REQUEST['answer']:'';	
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		foreach($answer AS $key => $value){
			if(is_array($value) && !empty($value)){
				$answer[$key] = implode(",", $value);
			}
		}
		$answer['dateline'] = strval(TIMESTAMP);
		$answer['user_id'] = $_G['user_id'];
		
		$insert_id = $this->add('answers',$answer);
		if($insert_id){
			$answer['dateline'] = strval(TIMESTAMP*1000);			
			$return['code'] = '0';
			$return['data']=$answer;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}
	
	/*
	 *  获取回答信息
	 */
	public function do_get_answers() {
	
		global $_G;
		$page = max(1, intval($_GET['page']));
		$perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '10';
		$start=(($page-1) * $perpage);
		$wheresql = "";
		$user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
		$only_me = isset($_REQUEST['only_me']) && !empty($_REQUEST['only_me']) ? $_REQUEST['only_me'] : '';
		if($user_id){
			$wheresql .= " AND user_id = '".$user_id."' ";
		}
								
		$orderby = " ORDER BY a_id DESC ";
		//DEBUG 初始化请求请求获取一页列表数据的参数
		$page_condition=array(
				'page' => $page,//int 请求页面 页码
				'limit' => $limit,//int 每页请求个数
				'perpage' => $perpage,//int 每页显示个数
				'wheresql' => $wheresql,//string //条件SQL语句
				'orderby' => $orderby,//string 排序规则
				'user_id' => $user_id,
				'only_me' => $only_me
		);
		$page_result = $this->get_answers('answers',$page_condition);		
		//DEBUG 获取各类信息分类名称 开始			 
		//DEBUG 返回输出
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		if($page_result){
			$return['code'] = '0';
			$return['data']=$page_result;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
            $return['data']=array();
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']); 
		}
	}
		
		
}
?>